package com.example.demo7;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.sql.Time;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    Calendar calendar = Calendar.getInstance();

    private TextView textView1,textView2,textView3;
    private ImageButton button1,button2;
    private Button button3,button4;
    private ImageView imageView1;
    private Switch aSwitch;
    private int currnview=0;

    private Button timebut,databut;
    private TextView timeTxt,dateTxt;
    private ToggleButton toggleButton;

    private LinearLayout dateln,timeln;

    private String xuanz,Xuanz1="";


    private int[] flag = new int[]{R.drawable.ytu_200,R.drawable.pku_200,R.drawable.tsinghua_200};
    private String[] flagname = new String[]{"烟台大学","北京大学","清华大学"};
    private boolean[] flagnameboolean = new boolean[]{false,false,false};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        textView1 = (TextView) findViewById(R.id.eduTxt);
        textView2 =(TextView) findViewById(R.id.xuanzeTxt);
        textView3 = (TextView) findViewById(R.id.tXt);
        timeTxt =(TextView)findViewById(R.id.timeTxt);
        dateTxt = (TextView)findViewById(R.id.dateTxt) ;

        button1 = (ImageButton) findViewById(R.id.syy);
        button2 = (ImageButton) findViewById(R.id.xyy);
        button3 = (Button) findViewById(R.id.but1);
        button4 = (Button) findViewById(R.id.btuuu2);

        imageView1 = (ImageView) findViewById(R.id.flagImageView);

        button1.setOnClickListener(xuanzhe);
        button2.setOnClickListener(xuanzhe);


        aSwitch = (Switch)findViewById(R.id.logoSizeSwitch);
        aSwitch.setOnCheckedChangeListener(onCheckedChangeListener);


        timebut = (Button)findViewById(R.id.timebut) ;
        timebut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimePickerDialog timePickerDialog=new TimePickerDialog(MainActivity.this, android.app.AlertDialog.THEME_HOLO_LIGHT, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int i, int i1) {
                        String shi = String.valueOf(i);
                        String fen = String.valueOf(i1);
                        timeTxt.setText(shi+"时"+fen+"分");

                    }
                },0,0,true);
                timePickerDialog.show();
            }
        });




        databut = (Button)findViewById(R.id.datebut) ;
        databut.setOnClickListener(new View.OnClickListener() {
            String nian,yue,ri;
            @Override
            public void onClick(View view) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                        nian = String.valueOf(i);
                        yue = String.valueOf(i1+1);
                        ri= String.valueOf(i2);
                        if(i1<10){
                            yue = "0"+(i1+1);
                        }
                        if(i2<10){
                            ri = "0"+(i2);
                        }
                        dateTxt.setText(nian+"年"+yue+"月"+ri+"日");
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
//                datePickerDialog.setButton(DatePickerDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        dateTxt.setText(nian+"年"+yue+"月"+ri+"日");
//                        dialogInterface.dismiss();
//                    }
//                });
            }
        });
        toggleButton = (ToggleButton) findViewById(R.id.togglebutton);

        toggleButton.setOnCheckedChangeListener(onCheckedChangeListener1);

        dateln = (LinearLayout)findViewById(R.id.datebn) ;
        timeln = (LinearLayout)findViewById(R.id.timebn) ;
        dateln.setVisibility(View.GONE);
        timeln.setVisibility(View.GONE);


        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder alerdialog = new AlertDialog.Builder(MainActivity.this);
                alerdialog.setIcon(R.drawable.university);
                alerdialog.setTitle("请选择喜欢的大学");
                alerdialog.setMultiChoiceItems(flagname, flagnameboolean, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        flagnameboolean[i] = b;
                    }
                });
                alerdialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        for(int j=0;j<flagname.length;j++){
                            if(flagnameboolean[j]){
                                Xuanz1 +=flagname[j].toString()+";";
                            }
                        }
                        if(Xuanz1.equals("")){
                            textView3.setText("希望你能找到你喜欢的大学");
                            dialogInterface.dismiss();
                        }
                        else{
                            textView3.setText(Xuanz1);
                            dialogInterface.dismiss();
                        }
                    }
                });
                alerdialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                alerdialog.create().show();
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder alerdialog = new AlertDialog.Builder(MainActivity.this);
                alerdialog.setIcon(R.drawable.university);
                alerdialog.setTitle("你的选择是");
                alerdialog.setSingleChoiceItems(flagname, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "你的大学是"+flagname[i], Toast.LENGTH_SHORT).show();
                        xuanz=flagname[i];
                    }
                });
                alerdialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        textView2.setText(xuanz);
                        dialogInterface.dismiss();
                    }
                });
                alerdialog.setNegativeButton("取消",null);
                alerdialog.create().show();
                //textView2.setText(textView1.getText().toString());
            }
        });


    }


    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener1 = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b){
                dateln.setVisibility(View.VISIBLE);
                timeln.setVisibility(View.VISIBLE);
            }
            else{
                dateln.setVisibility(View.GONE);
                timeln.setVisibility(View.GONE);
            }
        }
    };

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b){
                ViewGroup.LayoutParams params = imageView1.getLayoutParams();
                params.width=(int)getResources().getDimension(R.dimen.bigLogoSize);
                params.height=(int)getResources().getDimension(R.dimen.bigLogoSize);
                imageView1.setLayoutParams(params);
            }
            else{
                ViewGroup.LayoutParams params = imageView1.getLayoutParams();
                params.width=(int)getResources().getDimension(R.dimen.smallLogoSize);
                params.height=(int)getResources().getDimension(R.dimen.smallLogoSize);
                imageView1.setLayoutParams(params);
            }
        }
    };

    private View.OnClickListener xuanzhe = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int id = view.getId();
            switch (id){

                case R.id.syy:
                    if (currnview==0){
                        Toast.makeText(MainActivity.this, "已经是第一页", Toast.LENGTH_SHORT).show();
                        AlertDialog.Builder alerdialog = new AlertDialog.Builder(MainActivity.this);
                        alerdialog.setIcon(R.drawable.university);
                        alerdialog.setMessage("已经是第一页了");
                        alerdialog.setTitle("提示");
                        alerdialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                        alerdialog.create().show();
                        return;
                    }
                    else{
                        currnview--;
                        imageView1.setImageResource(flag[currnview]);
                        textView1.setText(flagname[currnview]);
                    }
                    break;

                case R.id.xyy:
                    if (currnview==flag.length-1){
                        Toast.makeText(MainActivity.this, "已经是最后一页", Toast.LENGTH_SHORT).show();
                        AlertDialog.Builder alerdialog = new AlertDialog.Builder(MainActivity.this);
                        alerdialog.setIcon(R.drawable.university);
                        alerdialog.setMessage("已经是最后一页了");
                        alerdialog.setTitle("提示");
                        alerdialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                        alerdialog.create().show();
                        return;
                    }
                    else{
                        currnview++;
                        imageView1.setImageResource(flag[currnview]);
                        textView1.setText(flagname[currnview]);
                    }
                    break;
                default:
                    break;
            }
        }
    };

}